#include <iostream>
using namespace std;
/**
 * 实现将Point类型的单例对象创建在堆区的写法，并完善
 * (1)需要提供回收单例对象的方法
 * (2)需要灵活使用单例对象，让单例对象的数据成员可以有不同的值
 */
class Point{
private:
    int _ix;
    int _iy;
    static Point *_pInstance;


public:
    static Point *getInstance(){
        if(_pInstance == nullptr){
            _pInstance = new Point(1, 8);
        }
        return _pInstance;
    }

    void init(int x, int y){
        this->_ix = x;
        this->_iy = y;
    }

    void print() const{
        cout << "(" << this->_ix
        << "," << this->_iy
        << ")" << endl;
    }
    // 没有this指针
    static void destroy(){
        if(_pInstance){
            delete _pInstance;
            _pInstance = nullptr;
        }
        cout << "delete" << endl;
    }


private:
    Point(int x,int y): _ix(x), _iy(y) {
        cout << "Point(int,int)" << endl;
    }
    ~Point(){
        cout << "~Point" << endl;
    }
    /**
     * 删除拷贝构造，赋值运算符
     */
    Point(Point &rhs) = delete;
    Point &operator=(const Point& rhs) = delete;
};
Point* Point::_pInstance = nullptr;

void test0(){
    Point::getInstance()->print();
    // cout << Point::getInstance() << endl;

    Point::getInstance()->init(2, 6);
    Point::getInstance()->print();

    Point::destroy();
    Point::destroy();
    Point::destroy();
    
}


int main(int argc, char *argv[]){
    test0();
    return 0;
}
